# -*- coding: utf-8 -*-

"""
@author: 猿小天
@contact: QQ:1638245306
@Created on: 2021/6/3 003 0:30
@Remark: 用户管理
"""
import hashlib

from django.contrib.auth.hashers import make_password
from django_restql.fields import DynamicSerializerMethodField
from rest_framework import serializers

from dvadmin.system.models import Users
from dvadmin.system.views.role import RoleSerializer, BaseRoleSerializer, get_user_role_extand_info
from dvadmin.utils.json_response import SuccessResponse, ErrorResponse, DetailResponse
from dvadmin.utils.serializers import CustomModelSerializer
from dvadmin.utils.validator import CustomUniqueValidator
from dvadmin.utils.viewset import CustomModelViewSet
from django_restql.mixins import QueryArgumentsMixin
from application import settings

default_user_password = settings.DEFAULT_USER_PASSWORD
login_user_info = settings.LOGIN_USER_INFO

class UserSerializer(CustomModelSerializer):
    """
    用户管理-序列化器
    """
    dept_name = serializers.CharField(source='dept.name', read_only=True)
    role_info = DynamicSerializerMethodField()

    class Meta:
        model = Users
        read_only_fields = ["id"]
        exclude = ['password']
        extra_kwargs = {
            'post': {'required': False},
        }

    def get_role_info(self, instance, parsed_query):
        roles = instance.role.all()

        # You can do what ever you want in here

        # `parsed_query` param is passed to BookSerializer to allow further querying
        serializer = BaseRoleSerializer(
            roles,
            many=True,
            parsed_query=parsed_query
        )
        return serializer.data


class UserCreateSerializer(CustomModelSerializer):
    """
    用户新增-序列化器
    """
    username = serializers.CharField(max_length=50,
                                     validators=[CustomUniqueValidator(queryset=Users.objects.all(), message="账号必须唯一")])
    password = serializers.CharField(required=False, default=make_password(
        hashlib.md5(default_user_password.encode(encoding='UTF-8')).hexdigest()))

    def save(self, **kwargs):
        data = super().save(**kwargs)
        # data.post.set(self.initial_data.get('post', []))
        return data

    class Meta:
        model = Users
        fields = "__all__"
        read_only_fields = ["id"]
        extra_kwargs = {
            'post': {'required': False},
        }


class UserUpdateSerializer(CustomModelSerializer):
    """
    用户修改-序列化器
    """
    username = serializers.CharField(max_length=50,
                                     validators=[CustomUniqueValidator(queryset=Users.objects.all(), message="账号必须唯一")])
    password = serializers.CharField(required=False, allow_blank=True)

    def save(self, **kwargs):
        data = super().save(**kwargs)
        post = self.initial_data.get('post', [])
        if len(post) > 0:
            data.post.set(post)
        return data

    class Meta:
        model = Users
        read_only_fields = ["id"]
        fields = "__all__"
        extra_kwargs = {
            'post': {'required': False, 'read_only': True},
        }


class UserViewSet(QueryArgumentsMixin, CustomModelViewSet):
    """
    用户接口
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = Users.objects.exclude(is_superuser=1).all()
    serializer_class = UserSerializer
    create_serializer_class = UserCreateSerializer
    update_serializer_class = UserUpdateSerializer
    filter_fields = ['name', 'username', 'gender', 'is_active', 'dept']
    # filter_fields = {
    #     'name': ['icontains'],
    #     'username': ['icontains'],
    #     'gender': ['icontains'],
    #     'is_active': ['icontains'],
    #     'dept': ['exact'],
    # }
    search_fields = ['username', 'name', 'gender', 'dept__name', 'role__name']

    def user_info(self, request):
        """获取当前用户信息"""
        user = request.user
        result = {}
        if user.id in login_user_info.keys():
            result = login_user_info[user.id]
        else:
            role_ids = []
            if user.role:
                for r in user.role.all():
                    role_ids.append(r.id)
            extand_info = get_user_role_extand_info(role_ids)
            result = {
                "user_id": user.id,
                "name": user.name,
                "mobile": user.mobile,
                "gender": user.gender,
                "email": user.email,
                "role": role_ids,
                "other": extand_info
            }
            login_user_info[user.id] = result
        return SuccessResponse(data=result, msg="获取成功")

    def update_user_info(self, request):
        """修改当前用户信息"""
        user = request.user
        Users.objects.filter(id=user.id).update(**request.data)
        return DetailResponse(data=None, msg="修改成功")

    def change_password(self, request, *args, **kwargs):
        """密码修改"""
        instance = Users.objects.filter(id=kwargs.get('pk')).first()
        data = request.data
        old_pwd = data.get('oldPassword')
        new_pwd = data.get('newPassword')
        new_pwd2 = data.get('newPassword2')
        if instance:
            if new_pwd != new_pwd2:
                return ErrorResponse(msg="两次密码不匹配")
            elif instance.check_password(old_pwd):
                instance.password = make_password(new_pwd)
                instance.save()
                return DetailResponse(data=None, msg="修改成功")
            else:
                return ErrorResponse(msg="旧密码不正确")
        else:
            return ErrorResponse(msg="未获取到用户")

    def password_reset(self, request, *args, **kwargs):
        """
            重置密码
            将密码重置为默认密码
        """
        instance = Users.objects.filter(id=kwargs.get('pk')).first()
        password = hashlib.md5(default_user_password.encode(encoding='UTF-8')).hexdigest()
        if instance:
            # print(hashlib.md5(password)
            instance.password = make_password(password)
            instance.save()
            return DetailResponse(data=None, msg="修改成功")
        else:
            return ErrorResponse(msg="未获取到用户")
